package com.yuanda.wordhot.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuanda.wordhot.entity.ConfigEntity;
import com.yuanda.wordhot.service.ConfigService;
import com.yuanda.wordhot.util.JsonUtils;

/**
 * 配置控制器
 * 
 * @author YuBai
 * @since 2013-3-18
 * @Description
 * @version 1.0
 */
@Controller
@RequestMapping("/admin")
public class ConfigController {
	@Autowired
	private ConfigService configService;

	@RequestMapping("getConfigs")
	@ResponseBody
	public String getConfigsByParent(String pid) {
		// 查询所有父节点为空的配置
		List<ConfigEntity> configs = configService.getConfigsByParent(pid);
		String json = JsonUtils.generateJsonByObject(configs);
		return json;
	}

	@RequestMapping("addConfig")
	@ResponseBody
	public String addConfig(ConfigEntity config) {
		// 新增加的节点都是叶子节点
		config.setLeaf(true);
		configService.addConfig(config);
		return "添加成功";
	}

	@RequestMapping("updateConfig")
	@ResponseBody
	public String updateConfig(ConfigEntity config) {
		configService.updateConfig(config);
		return "更新成功";
	}

	@RequestMapping("deleteConfig")
	@ResponseBody
	public String deleteConfig(String configId) {
		configService.deleteConfig(configId);
		return "删除成功";
	}
}
